package com.xiaobawang.hundouluo.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.mob.wrappers.UMSSDKWrapper;
import com.xiaobawang.hundouluo.bean.NetCallback;
import com.xiaobawang.hundouluo.utils.SharedPreferencesUtil;
import com.xiaobawang.hundouluo.utils.UrlUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

//登录校验、注册、重置密码 通过传输给该Service的type值进行确定
public class UserService extends IntentService {

    public UserService() {
        super("UserService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            String type = intent.getStringExtra("type");
            assert type != null;
            if (type.equals("login")) {
                String userName = intent.getStringExtra("username");
                String password = intent.getStringExtra("password");
                Log.e("UserService", "login======>" + userName + "," + password);
                login(userName, password);
            } else if (type.equals("register")) {
                String userName = intent.getStringExtra("username");
                String password = intent.getStringExtra("password");
                String phone = intent.getStringExtra("phone");
                Log.e("UserService", "register======>" + userName + "," + password + "," + phone);
                register(userName, password, phone);
            } else if (type.equals("checkUser")) {
                String userName = intent.getStringExtra("username");
                String phone = intent.getStringExtra("phone");
                Log.e("UserService", "checkUser======>" + userName + "," + phone);
                checkUser(userName, phone);
            } else if (type.equals("changePassword")) {
                String userName = intent.getStringExtra("username");
                String password = intent.getStringExtra("password");
                Log.e("UserService", "changePassword======>" + userName + "," + password);
                changePassword(userName, password);
            } else if (type.equals("repass")) {
                String newPassword = intent.getStringExtra("newpass");
                Log.e("UserService", "repass======>" + newPassword);
                rePassword(newPassword);
            }
        }
    }

    //登录逻辑处理函数
    private void login(String userName, String password) {

        //网络请求进行结果校验
        RequestParams params = new RequestParams(UrlUtil.LOGIN);
        params.addBodyParameter(UrlUtil.PARAM_NAME, userName);
        params.addBodyParameter(UrlUtil.PARAM_PASS, password);

        //发送网络请求
        x.http().post(params, new NetCallback("UserLogin") {

            @Override
            public void onSuccess(String result) {

                Log.e("Net-Login", result);
                //创建JSON根部对象
                try {
                    JSONObject rootObject = new JSONObject(result);
                    JSONObject user = rootObject.getJSONObject("user");
                    String status = rootObject.getString("status");
                    String uid = user.getString("uid");
                    String uname = user.getString("uname");
                    if (status.equalsIgnoreCase("success")) {
                        SharedPreferencesUtil.write(UserService.this, "userLogin", "uid", uid);
                        SharedPreferencesUtil.write(UserService.this, "userLogin", "uname", uname);
                    }
                    //发送广播
                    Intent bcIntent = new Intent("com.xiaobawang.hundouluo.receiver.login");
                    bcIntent.putExtra("loginResult", status);
                    sendBroadcast(bcIntent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    //注册处理逻辑函数
    private void register(String userName, String password, String phone) {
        //网络请求进行结果校验
        RequestParams params = new RequestParams(UrlUtil.REGISTER);
        params.addBodyParameter(UrlUtil.PARAM_NAME, userName);
        params.addBodyParameter(UrlUtil.PARAM_PASS, password);
        params.addBodyParameter(UrlUtil.PARAM_PHONE, phone);

        //发送网络请求
        x.http().post(params, new NetCallback("UserRegister") {
            @Override
            public void onSuccess(String result) {

                Log.e("Net-Register", result);
                try {
                    JSONObject rootObject = new JSONObject(result);
                    JSONObject user = rootObject.getJSONObject("results");
                    String status = rootObject.getString("status");
                    String uid = user.getString("uid");
                    String uname = user.getString("uname");
                    String uphone = user.getString("uphone");
                    if (status.equalsIgnoreCase("success")) {
                        SharedPreferencesUtil.write(UserService.this, "userRegister", "uid", uid);
                        SharedPreferencesUtil.write(UserService.this, "userRegister", "uname", uname);
                        SharedPreferencesUtil.write(UserService.this, "userRegister", "uname", uphone);
                    }
                    //发送广播
                    Intent bcIntent = new Intent("com.xiaobawang.hundouluo.receiver.register");
                    bcIntent.putExtra("registerResult", status);
                    sendBroadcast(bcIntent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        });
    }

    private void checkUser(String userName, String phone) {
        //网络请求进行结果校验
        RequestParams params = new RequestParams(UrlUtil.CHECK);
        params.addBodyParameter(UrlUtil.PARAM_NAME, userName);
        params.addBodyParameter(UrlUtil.PARAM_PHONE, phone);

        //发送网络请求
        x.http().post(params, new NetCallback("UserCheck") {
            @Override
            public void onSuccess(String result) {

                Log.e("Net-CheckUser", result);
                try {
                    JSONObject rootObject = new JSONObject(result);
                    JSONObject user = rootObject.getJSONObject("results");
                    String status = rootObject.getString("status");
                    String uname = user.getString("uname");
                    String uphone = user.getString("uphone");
                    if (status.equalsIgnoreCase("success")) {
                        SharedPreferencesUtil.write(UserService.this, "userCheck", "uname", uname);
                        SharedPreferencesUtil.write(UserService.this, "userCheck", "uphone", uphone);
                    }
                    //发送广播
                    Intent bcIntent = new Intent("com.xiaobawang.hundouluo.receiver.userCheck");
                    bcIntent.putExtra("userCheckResult", status);
                    sendBroadcast(bcIntent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        });
    }

    private void changePassword(String userName, String password) {
        //网络请求进行结果校验
        RequestParams params = new RequestParams(UrlUtil.CHANGE_PASSWORD);
        params.addBodyParameter(UrlUtil.PARAM_NAME, userName);
        params.addBodyParameter(UrlUtil.PARAM_PASS, password);

        //发送网络请求
        x.http().post(params, new NetCallback("ChangePassword") {
            @Override
            public void onSuccess(String result) {

                Log.e("Net-PasswordChange", result);
                try {
                    JSONObject rootObject = new JSONObject(result);
                    JSONObject user = rootObject.getJSONObject("results");
                    String status = rootObject.getString("status");
                    String uname = user.getString("uname");
                    String upwd = user.getString("upwd");
                    if (status.equalsIgnoreCase("success")) {
                        SharedPreferencesUtil.write(UserService.this, "userChangePassword", "uname", uname);
                        SharedPreferencesUtil.write(UserService.this, "userChangePassword", "upwd", upwd);
                    }
                    //发送广播
                    Intent bcIntent = new Intent("com.xiaobawang.hundouluo.receiver.changePassword");
                    bcIntent.putExtra("changePasswordResult", status);
                    sendBroadcast(bcIntent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        });
    }

    private void rePassword(String newPassword) {
        RequestParams params = new RequestParams(UrlUtil.REPASS);
        params.addBodyParameter(UrlUtil.PARAM_NAME, SharedPreferencesUtil.read(UserService.this, "userInfo", "username"));
        params.addBodyParameter(UrlUtil.PARAM_PASS, newPassword);
        params.addBodyParameter(UrlUtil.PARAM_ID, SharedPreferencesUtil.read(UserService.this, "userLogin", "uid"));
        x.http().post(params, new NetCallback("UserRePassword") {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject rootObj = new JSONObject(result);
                    String status = rootObj.getString("status");
                    Intent rpIntent = new Intent("com.xiaobawang.hundouluo.receive.rePass");
                    rpIntent.putExtra("rePassResult", status);
                    sendBroadcast(rpIntent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}



